package com.pocket.demo.core.mq.rabbitMQ.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;

/**
 * @author cookie. 【demo-MQ-rabbitMQ-4】
 **/
//@Configuration
//@Import({TopicPublisher.class, TopicConsumer.class})
public class TopicExchangeConfig {
    private static final Logger LOGGER = LoggerFactory.getLogger(TopicExchangeConfig.class);

    public static final String TOPIC_EXCHANGE = "pocket.topic";
    public static final String TOPIC_QUEUE_1 = "pocket.topic.queue_1";
    public static final String TOPIC_QUEUE_2 = "pocket.topic.queue_2";

    public static final String TOPIC_ROUTING_ERROR_KEY = "#.error";
    public static final String TOPIC_ROUTING_WARN_KEY = "#.warn";

    @Bean
    public MessageConverter messageConverter(){
        // 1.定义消息转换器
        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter();
        // 2.配置自动创建消息id，用于识别不同消息，也可以在业务中基于ID判断是否是重复消息
        jackson2JsonMessageConverter.setCreateMessageIds(true);
        return jackson2JsonMessageConverter;
    }

    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(TOPIC_EXCHANGE, true, false);
    }

    @Bean
    public Queue topicQueue1(){
        return new Queue(TOPIC_QUEUE_1, true, false, false);
    }

    @Bean
    public Queue topicQueue2(){
        return new Queue(TOPIC_QUEUE_2, true, false, false);
    }

    @Bean
    public Binding bindingQueue1(Queue topicQueue1, TopicExchange topicExchange){
        return BindingBuilder.bind(topicQueue1).to(topicExchange).with(TOPIC_ROUTING_ERROR_KEY);
    }

    @Bean
    public Binding bindingQueue2(Queue topicQueue2, TopicExchange topicExchange){
        return BindingBuilder.bind(topicQueue2).to(topicExchange).with(TOPIC_ROUTING_WARN_KEY);
    }

}
